<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<head>

</head>
<body>
	<div class="x-nav">
		<div class="select" >
			<span class="layui-breadcrumb" style="padding-top: 5px;display: inline-block;">
			  <a href="javascript:void(0)">首页</a><a href="javascript:void(0)">系统管理</a><a><cite>用户管理</cite></a>
			</span>
			<button class="layui-btn layui-btn-sm" id="refresh" style="float: right;" data-type="reload"><i class="layui-icon">ဂ</i></button>
		</div>
	</div>
	<div class="layui-col-md12" style="height:40px;margin-top:3px;">
		<form class="layui-form" action="javaScript:void(0);" id="indexSerchForm">
			账号:<div class="layui-inline">
					<input class="layui-input" height="20px" id="account" autocomplete="off">
				</div>
			昵称:<div class="layui-inline">
				<input class="layui-input" height="20px" id="nickName" autocomplete="off">
			</div>
			用户类型:<div class="layui-inline">
					<select  class="select" id="userType" name="userType" lay-filter="aihao">
						<option value="">请选择</option>
						<option value="0" <c:if test="${entity.userType == '0'}">selected</c:if>>普通用户</option>
						<option value="1" <c:if test="${entity.userType == '1'}">selected</c:if>>教师</option>
						<option value="2" <c:if test="${entity.userType == '2'}">selected</c:if>>管理员</option>
					</select>
			</div>
			<div class="layui-btn-group" style="float: right;margin-right: 20px;">
				<button class="select-on layui-btn layui-btn-sm" data-type="select"><i class="layui-icon"></i></button>
				<button class="select-on layui-btn layui-btn-sm" data-type="reload"><i class="layui-icon">&#x1007;</i>重置</button>
				<button class="select-on layui-btn layui-btn-sm" data-type="add"><i class="layui-icon">&#xe608;</i>新增</button>
				<button class="select-on layui-btn layui-btn-sm" data-type="dels"><i class="layui-icon">&#xe640;</i>删除</button>
			</div>
		</form>
	</div>
	<table id="pageList" class="layui-hide" lay-filter="user"></table>
	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
		<a class="layui-btn layui-btn-xs  layui-btn-normal" lay-event="edit">编辑</a>
		{{# if(d.userType!="2"){ }}
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" id="delBtn">删除</a>
		{{#  } }}
		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="changePwd">重置密码</a>
	</script>
</body>
<myfooter>
	<script>
		function reload(table) {
            table.reload('pageList', {
                where: {
                    search_LIKE_account: $("#account").val(),
                    search_LIKE_nickName:  $("#nickName").val(),
                    search_EQ_userType: $("#userType").val()
                }
            });
        }
        var queryUrl = "${ctx}/user/queryByPage";
        var delUrl = "${ctx}/user/deleteEntity";
		var saveUrl = "${ctx}/user/findOne";

        layui.use('table', function () {
            var table = layui.table;
            //方法级渲染
            table.render({
                id: 'pageList',
                elem: '#pageList',//指定原始表格元素选择器（推荐id选择器）
                url: queryUrl,
                cols: [[
                    {checkbox: true, fixed: true, width: '5%'},
                    {field: 'account',title: '账户',width: '10%'},
                    {field: 'nickName', title: '用户昵称', width: '17%'},
                    {field: 'userType', title: '用户类型', width: '20%',
                        templet: function(d){
                            return getDictLabel(${fns:keyValList("userTypeEnumPath")},d.userType,"未知");
                        }
                    },
                    {field: 'available', title: '状态', width: '13%',
                        templet: function(data){
                            if(data.available=='false'){
                                return '正常';
                            }else if(data.available=='true'){
                                return '锁定';
                            }else {
                                return "未知";
                            }
                        }
                    },
                    {field: 'createDate', title: '创建时间', width: '15%',sort: true},
                    {field: 'right', title: '操作', width: '20%', toolbar: "#barDemo"}
                ]],
                page: true,
                height: 'full-105'
            });
            var $ = layui.$,
			active = {
                select: function () {
                    reload(table);
                },
                reload:function(){
					$("#indexSerchForm").find('input').val("");
                    $("#indexSerchForm").find('select').val("");
                    reload(table);
                },
                add: function () {
                    layerSave('添加用户', saveUrl+"?1=1", 900, 450);
                },
                dels:function(){
                    var checkStatus = table.checkStatus('pageList'),
						data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg('请至少选择一行,已选['+data.length+']行', {icon: 5});
                        return false;
                    }
                    var ids="";
                    for(var i =0;i<data.length;i++){
                      ids=ids + data[i].id+",";
					}
                    console.log(ids);
                    layer.confirm('确定删除['+data.length+']行用户?', {
                        btn: ['逻辑删除', '物理删除']
                    }, function (index) {
                        layerDels(delUrl,ids, true);
                        reload(table);
                        layer.close(index);
                    }, function (index) {
                        layerDels(delUrl,ids, false);
                        reload(table);
                        layer.close(index);
                    });
                }
			};
            //监听工具条
            table.on('tool(user)', function (obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    layerDetail('查看用户', saveUrl+'?id=' + data.id, 900, 450);
                } else if (obj.event === 'del') {
                    layer.confirm('确定删除用户[<label style="color: #00AA91;">' + data.nickName + '</label>]?', {
                        btn: ['逻辑删除', '物理删除']
                    }, function (index) {
                        layerDel(delUrl,data.id, true);
                        reload(table);
                        layer.close(index);
                    }, function (index) {
                        layerDel(delUrl,data.id, false);
                        reload(table);
                        layer.close(index);
                    });

                } else if (obj.event === 'edit') {
                    layerSave('编辑用户', saveUrl+'?id=' + data.id, 900, 450);
                } else if (obj.event === 'changePwd') {
                    layer.confirm('确定重置用户[<label style="color: #00AA91;">' + data.nickName + '</label>]密码?',function (index) {
                        $.ajax({
                            url:"${ctx}/user/resetLoginPwd",
                            type:'post',
                            data:{"id": data.id},
                            dataType:"json",
                            cache:false,
                            async:false,
                            success:function(data){
                                if(data.state == true || data.flag==200){
                                    window.top.layer.msg('成功',{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
//                                    window.top.layer.msg('!', {icon:1,time:2000});
                                }else{
                                    window.top.layer.msg('操作失败!', {time: 20000,btn: ['知道了']});
                                }
                                layer.close(index);
                            },error:function(){
                                layer.msg('操作错误!', {time: 20000,btn: ['知道了']});
                                layer.close(index);
                            }
                        });
                    });
                }
            });
            /*头部按钮和刷新按钮的点击触发*/
            $('.layui-col-md12 .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            $('.select .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
	</script>
</myfooter>
